
import * as echarts from "echarts";
export class ChartUtils {
    constructor() {
        this.dom = null;
        this.chart = null;
        this.s1 = 5 * 60;
        this.s2 = 0;
        this.option = this.get_default_options();
        this.intervel = null;
        this.evs = [];
    }
    addCall(func) {
        this.evs.push(func);
    }
    setDom(dom) {
        this.dom = dom;
        this.chart = echarts.init(dom);
    }
    setOption(opt) {
        this.option = Object.assign(this.option, opt);
        this.chart.setOption(this.option);
    }
    async emit() {
        for (let i = 0; i < this.evs.length; i++) {
            new Promise((r, v) => {
                try {
                    this.evs[i]({
                        start: this.option.dataZoom[0].startValue,
                        end: this.option.dataZoom[0].endValue,
                    });
                    r('success!');
                } catch (e) {
                    v(e);
                }
            })
                .then(console.log)
                .catch(console.log);
        }
    }



    get_default_options() {
        return {
            tools: {
                feature: {
                    saveAsImage: {},
                },
            },
            tooltip: {
                trigger: "axis",
                axisPointer: {
                    type: "cross",
                },
                animation: false
            },
            
        };
    }
}

export function toFixed(x = 0.0, n = 2) {
    let xx = x.toFixed(n)
    let zz = parseFloat(xx)
    return zz
}
// 传入nolabel控制纵坐标名称与数值显示
export function generateYAxis({ type = 'value', nolabel = false, label = '', unit = '', min = null, max = null } = {}) {

    let yaxis = {
        type: type,
        axisLabel: {
            // formatter: `{value} ${unit}`,
            formatter: nolabel == true ? `{value}${unit}` : `{value}`,
            // 设置 Y轴坐标轴颜色，默认为白色
            textStyle: {
                color: '#5586be'
            }
        },
        // name: label +'\n('+ unit +')',
        // 设置 Y轴坐标轴名称
        name: nolabel == true ? '' : label + unit,
        nameLocation: "middle",
        nameRotate: 90,
        nameGap: 60,
        nameTextStyle: {
            color: '#5586be',
            align: "center",
            verticalAlign: "top",
            fontSize: 15
        },
        splitLine: {
            show: false
        },
        axisPointer: {
            snap: false,
        },
    }

    if (type === 'value') {

        yaxis['min'] = min === null ? function (value) { return toFixed(value.min) } : min
        yaxis['max'] = max === null ? function (value) { return toFixed(value.max) } : max

        yaxis['min'] = function (value) {
            let rmin = value.min
            if(value.min < 0.1) {
                rmin = value.min - 0.02
            }
            if(value.min < 1) {
                rmin = value.min - 0.2
            }
            if(value.min >= 1){
                rmin = value.min* 0.98
            }
            if(rmin < 0){
                rmin = 0
            }
            return parseFloat(rmin.toFixed(2))
        }
        yaxis['max'] = function (value) {
            let rmax = value.max
            if(value.max < 0.1) {
                rmax = value.max + 0.02
            }
            else if(value.max < 1) {
                rmax = value.max + 0.2
            }
            if(value.max >= 1){
                rmax = value.max* 1.02
            }   
            
            return parseFloat(rmax.toFixed(2))

            // return value.max + 0.02
        }
    }

    return yaxis

}

export function generateXAxis(len) {
    let now = new Date();
    let xaxis = {
        data: Array(len).fill('').map((v, i) => {
            let d = new Date(now.getTime() - len * 1000 + i * 1000 - 5000);
            return d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds();

        }),
        axisLabel: {
            align: 'center',
            rotate: -20,
            // showMinLabel: true,
            // showMaxLabel: true,
            margin: 20,
            hideOverlap: false,
            // 设置 X轴坐标轴颜色，默认为白色
            textStyle: {
                color: 'white'
            }
        },
        interval: 1,
        minInterval: 1,
        maxInterval: 1,
        inside: true,
        splitNumber: 1,
        axisTick: {
            alignWithLabel: true,
            interval: 1,
            maxInterval: 1,
        },
        axisPointer: {
            snap: true,
        },
    }
    return xaxis
}